/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI/TextMessages/FadingMessage.h"

namespace TD_GUI {

	/************************************************************************/
	/* Default definitions                                                  */
	/************************************************************************/
	const int FadingMessage::defaultHoldingTime = 2000; //2 secs
	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	FadingMessage::FadingMessage() : BaseTextMessage() {

	}

	FadingMessage::FadingMessage(const FadingMessage &rhs) : BaseTextMessage(rhs) {

	}

	FadingMessage & FadingMessage::operator =(const FadingMessage &rhs) {
		BaseTextMessage::operator =(rhs);
		m_text = rhs.getText();
		return(*this);
	}

	FadingMessage::FadingMessage(const Coord2D &textPos, const string &initString, const string &fontSource, 
		unsigned int fontSize, const Color &fontColor, bool gFadingIn, int holdTime, renderType g_renderType) 
		: BaseTextMessage(textPos, initString, fontSource, fontSize, fontColor, textSolid) {
			m_fadingIn = gFadingIn;
			m_alphaChannel = m_fontColor.unused;
			m_fadedAnimation = false;
			m_ticks = 0;
			m_timeBeforeFade = holdTime;
	}

	FadingMessage::~FadingMessage() {

	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/

	void FadingMessage::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		m_ticks += deltaTime;
		if(!m_fadedAnimation && m_ticks >= m_timeBeforeFade) {
			if(m_fadingIn) {
				m_alphaChannel += 4;
				if(m_alphaChannel  + 4 >= 255) {
					m_alphaChannel = 255;
					m_fadedAnimation = true;
				}
			} else {
				m_alphaChannel -= 4;
				if(m_alphaChannel  - 4 <= 0) {
					m_alphaChannel = 0;
					m_fadedAnimation = true;
				}
			}
		}
		m_fontColor.unused = m_alphaChannel;
		setText(m_text);
		blitSurface(g_destSurface);
	}
}